#ifndef _TH25D_40UB_H
#define _TH25D_40UB_H

#include "stdint.h"
#include "t_spi_m.h"

#ifdef __cplusplus
extern "C" {
#endif

/* 基本控制命令 */
#define TH25D_CMD_WRITE_ENABLE              (0x06)
#define TH25D_CMD_WRITE_DISABLE             (0x04)
#define TH25D_CMD_VOLATILE_SR_WRITE_ENABLE  (0x50)
#define TH25D_CMD_ENABLE_RESET              (0x66)
#define TH25D_CMD_RESET                     (0x99)
#define TH25D_CMD_DEEP_POWER_DOWN           (0xB9)
#define TH25D_CMD_CONTINUOUS_READ_MODE_RESET (0xFF)

/* 状态寄存器命令 */
#define TH25D_CMD_READ_STATUS_REG           (0x05)
#define TH25D_CMD_READ_STATUS_REG_1         (0x35)
#define TH25D_CMD_WRITE_STATUS_REG          (0x01)

/* 读操作命令 */
#define TH25D_CMD_READ_DATA                 (0x03)
#define TH25D_CMD_FAST_READ                 (0x0B)
#define TH25D_CMD_DUAL_OUTPUT_FAST_READ     (0x3B)
#define TH25D_CMD_DUAL_IO_FAST_READ         (0xBB)

/* 编程/擦除控制 */
#define TH25D_CMD_PROGRAM_ERASE_SUSPEND     (0x75)  // 或 0xB0
#define TH25D_CMD_PROGRAM_ERASE_RESUME      (0x7A)  // 或 0x30

/* 编程命令 */
#define TH25D_CMD_PAGE_PROGRAM              (0x02)
#define TH25D_CMD_DUAL_INPUT_PAGE_PROGRAM   (0xA2)

/* 擦除命令 */
#define TH25D_CMD_SECTOR_ERASE_05K          (0x8A)
#define TH25D_CMD_SECTOR_ERASE_4K           (0x20)
#define TH25D_CMD_BLOCK_ERASE_32K           (0x52)
#define TH25D_CMD_BLOCK_ERASE_64K           (0xD8)

/* ID读取命令 */
#define TH25D_CMD_RELEASE_DP_READ_ID        (0xAB)
#define TH25D_CMD_MANUFACTURER_DEVICE_ID    (0x90)
#define TH25D_CMD_MANUFACTURER_DEVICE_ID_DUAL_IO (0x92)
#define TH25D_CMD_READ_UNIQUE_ID            (0x4B)
#define TH25D_CMD_READ_SFDP                 (0x5A)
#define TH25D_CMD_READ_IDENTIFICATION       (0x9F)

/* 安全寄存器命令 */
#define TH25D_CMD_ERASE_SECURITY_REG        (0x44)
#define TH25D_CMD_PROGRAM_SECURITY_REG      (0x42)
#define TH25D_CMD_READ_SECURITY_REG         (0x48)

/* 状态寄存器位定义 */
#define TH25D_STATUS_WIP                    (0x01)  // Write In Progress
#define TH25D_STATUS_WEL                    (0x02)  // Write Enable Latch
#define TH25D_STATUS_BP0                    (0x04)  // Block Protect 0
#define TH25D_STATUS_BP1                    (0x08)  // Block Protect 1
#define TH25D_STATUS_BP2                    (0x10)  // Block Protect 2
#define TH25D_STATUS_SRWD                   (0x80)  // Status Register Write Disable

/* 状态寄存器位（高字节部分）*/
#define TH25D_STATUS_SUS1                   (0x8000)  // 擦除挂起
#define TH25D_STATUS_CMP                    (0x4000)  // 区域保护对比位
#define TH25D_STATUS_LB3                    (0x2000)  // 安全寄存器锁定位
#define TH25D_STATUS_LB2                    (0x1000)
#define TH25D_STATUS_LB1                    (0x0800)
#define TH25D_STATUS_SUS2                   (0x0400)  // 编程挂起
#define TH25D_STATUS_SRP1                   (0x0200)
#define TH25D_STATUS_SRP0                   (0x0080)

/* 地址模式 */
#define TH25D_ADDRESS_MODE_24BIT            (0)
#define TH25D_ADDRESS_MODE_32BIT            (1)

/* 其他参数 */
#define TH25D_DUMMY_BYTE                    (0xFF)
#define TH25D_PAGE_SIZE                     (256)   // 页编程大小
#define TH25D_SECTOR_SIZE_05K               (512)   // 0.5KB扇区
#define TH25D_SECTOR_SIZE_4K                (4096)  // 4KB扇区
#define TH25D_BLOCK_SIZE_32K                (32768) // 32KB块
#define TH25D_BLOCK_SIZE_64K                (65536) // 64KB块
#define TH25D_SECURITY_REG_SIZE             (512)   // 安全寄存器大小

/* 安全寄存器编号 */
#define TH25D_SECURITY_REG_1                (1)
#define TH25D_SECURITY_REG_2                (2)
#define TH25D_SECURITY_REG_3                (3)

/* 错误代码定义 */
typedef enum {
    TH25D_40UB_OK = 0,            // 操作成功
    TH25D_40UB_ERR_INVAL = 1,     // 无效参数
    TH25D_40UB_ERR_TIMEOUT = 2,   // 操作超时
    TH25D_40UB_ERR_WRITE = 3,     // 写入失败
    TH25D_40UB_ERR_READ = 4,      // 读取失败
    TH25D_40UB_ERR_BUSY = 5,      // 设备忙
    TH25D_40UB_ERR_PROTECTED = 6, // 受保护区域
    TH25D_40UB_ERR_NOT_ERASED = 7,// 未擦除区域
    TH25D_40UB_ERR_SIZE = 8,      // 大小错误
    TH25D_40UB_ERR_LOCKED = 9,    // 寄存器已锁定
    TH25D_40UB_ERR_NOT_SUPPORT = 10 // 不支持的功能
} t_flash_erro_t;

/* CS引脚控制结构体 */
typedef struct {
    void *cs_port;
    uint16_t cs_pin; // CS pin
} t_cs_gpio_t;

/* SPI读写函数指针类型 */
typedef void (*spi_flash_read_f)(void *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
typedef void (*spi_flash_write_f)(void *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
typedef void (*spi_cs_write_func)(void *port, uint16_t pin, uint8_t pin_val);

/* 闪存控制器结构体 */
typedef struct {
    void *arg;                  // 传入的参数(通常是SPI句柄)
    const t_cs_gpio_t *hw_gpio; // GPIO配置(为NULL时使用硬件NSS模式)
    spi_cs_write_func hw_func;  // GPIO配置输出函数
    spi_flash_read_f read_func; // SPI读取函数指针
    spi_flash_write_f write_func; // SPI写入函数指针
} th25d_40ub_t;

/**
 * @brief 获取闪存容量(字节)
 * @return 闪存容量(512KB)
 */
#define TH25D_40UB_CAPACITY (512 * 1024)

/* ========== 设备初始化与销毁 ========== */
th25d_40ub_t *th25d_40ub_create(void *arg,
                               spi_flash_read_f read_func,
                               spi_flash_write_f write_func,
                               spi_cs_write_func hw_func,
                               const t_cs_gpio_t *hw_gpio);

void th25d_40ub_destroy(th25d_40ub_t *dev);

/* ========== 设备识别与状态 ========== */
t_flash_erro_t th25d_40ub_read_id(th25d_40ub_t *dev, uint8_t *mid, uint8_t *did);
t_flash_erro_t th25d_40ub_read_unique_id(th25d_40ub_t *dev, uint8_t uid[16]);
t_flash_erro_t th25d_40ub_read_status(th25d_40ub_t *dev, uint16_t *status);
t_flash_erro_t th25d_40ub_write_status(th25d_40ub_t *dev, uint16_t status);
t_flash_erro_t th25d_40ub_wait_ready(th25d_40ub_t *dev, uint32_t timeout);

/* ========== 基本读写操作 ========== */
t_flash_erro_t th25d_40ub_read(th25d_40ub_t *dev, uint32_t addr, uint8_t *data, uint32_t size);
t_flash_erro_t th25d_40ub_write(th25d_40ub_t *dev, uint32_t addr, const uint8_t *data, uint32_t size);
t_flash_erro_t th25d_40ub_fast_read(th25d_40ub_t *dev, uint32_t addr, uint8_t *data, uint32_t size);
t_flash_erro_t th25d_40ub_dual_io_read(th25d_40ub_t *dev, uint32_t addr, uint8_t *data, uint32_t size);

/* ========== 擦除操作 ========== */
t_flash_erro_t th25d_40ub_erase_05k_sector(th25d_40ub_t *dev, uint32_t addr);
t_flash_erro_t th25d_40ub_erase_4k_sector(th25d_40ub_t *dev, uint32_t addr);
t_flash_erro_t th25d_40ub_erase_32k_block(th25d_40ub_t *dev, uint32_t addr);
t_flash_erro_t th25d_40ub_erase_64k_block(th25d_40ub_t *dev, uint32_t addr);
t_flash_erro_t th25d_40ub_chip_erase(th25d_40ub_t *dev);

/* ========== 电源管理 ========== */
t_flash_erro_t th25d_40ub_enter_deep_powerdown(th25d_40ub_t *dev);
t_flash_erro_t th25d_40ub_release_deep_powerdown(th25d_40ub_t *dev);

/* ========== 安全寄存器操作 ========== */
t_flash_erro_t th25d_40ub_erase_security_reg(th25d_40ub_t *dev, uint8_t reg_num);
t_flash_erro_t th25d_40ub_program_security_reg(th25d_40ub_t *dev, uint8_t reg_num, const uint8_t *data);
t_flash_erro_t th25d_40ub_read_security_reg(th25d_40ub_t *dev, uint8_t reg_num, uint8_t *data);
t_flash_erro_t th25d_40ub_is_security_reg_locked(th25d_40ub_t *dev, uint8_t reg_num, uint8_t *locked);

/* ========== 高级控制功能 ========== */
t_flash_erro_t th25d_40ub_software_reset(th25d_40ub_t *dev);
t_flash_erro_t th25d_40ub_reset_continuous_read(th25d_40ub_t *dev);
t_flash_erro_t th25d_40ub_suspend(th25d_40ub_t *dev);
t_flash_erro_t th25d_40ub_resume(th25d_40ub_t *dev);

#ifdef __cplusplus
}
#endif

#endif /* _TH25D_40UB_H */